﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerMSMQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //本地发送MessageQueue mq = new MessageQueue(@".\Private$\susuMSMQ");
            //tcp发送new MessageQueue(@"FormatName:Direct=tcp:localhoost\Private$\kkkMQ");
            //局域网发送new MessageQueue(@"FormatName:Direct=http://localhost/msmq/Private$/kkkMQ");
            //局域网发送new MessageQueue(@"FormatName:Direct=http://192.168.1.100/msmq/Private$/kkkMQ");
            //使用内网穿透域名发送new MessageQueue(@"FormatName:Direct=http://susumsmq.frpgz1.idcfengye.com/msmq/Private$/kkkMQ");

            //MQ的名字，也可以使用上面的方法建立
            string mqName = $".\\private$\\{textBox1.Text.Trim()}";
            //string mqName = @"FormatName:Direct=TCP:192.168.1.101\Private$\mqtest";

            //如果不存在mqName消息队列，则创建该mqName消息队列  
            if (MessageQueue.Exists(mqName))
            {
                MessageBox.Show("该消息队列已经存在！");
            }
            else
            {
                MessageQueue.Create(mqName);

                MessageQueue queue = new MessageQueue(mqName);
                queue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);    //加权限
                queue.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
                //queue.MaximumQueueSize = CommonSettings.QueueMaxSize
                MessageBox.Show("该消息队列创建成功！");
            }
        }
    }
}
